package config

import (
	"os"
	"path/filepath"
	"runtime"

	"github.com/sirupsen/logrus"
)

//目前toml的配置文件主要是为本地调试而产生的
func ConfigFilePath(fileNames ...string) (filePath string) {
	var (
		fileName = "config.toml"
	)

	if len(fileNames) > 0 {
		fileName = fileNames[0]
	}

	//从环境变量解析
	if filePath = os.Getenv("CONFIG_PATH"); filePath != "" {
		return
	}

	//if *configFilePath != "" {
	//	filePath = *configFilePath
	//	return
	//}

	//从调用栈解析配置文件路径
	_, file, _, ok := runtime.Caller(1)
	if !ok {
		logrus.Infof("Can not get current file info")
		return
	}
	var err error
	filePath, err = filepath.Abs(file + "/../../../../configs/" + fileName)
	if err != nil {
		logrus.Infof("toml config path err:%v", err)
		return
	}

	return
}

func MigrateRootDirPath() (filePath string) {
	defer func() {
		if filePath != "" {
			logrus.Infof("migrate path:%s", filePath)
		}
	}()

	//从调用栈解析配置文件路径
	_, file, _, ok := runtime.Caller(1)
	if !ok {
		logrus.Infof("Can not get current file info")
		return
	}
	var err error
	filePath, err = filepath.Abs(file + "/../../../../migrates")
	if err != nil {
		logrus.Infof("migrates path err:%v", err)
		return
	}
	return
}
